from collections import defaultdict
import csv

number_of_ranks = 6

if __name__ == "__main__":

	a = open("SowiportShuffled.csv","r").readlines()
	e = []
	for x in a:
		e.append(x.split("\t"))

	#Dict of <set id, details of entry>
	entries = defaultdict(list)
	for x in e:
		if x[1] in entries:
			entries[x[1]].append(x)
		else:
			entries[x[1]] = [x]


	p = []
	s = []
	
	for rank in range(1,number_of_ranks+1):
		relevant_sets = defaultdict(list)
		# For each set
		for x in entries.keys():
			p = []
			s = []
			for y in entries[x]:
				p.append(y[3]) #Rank delivered (may be shuffled)
				s.append(y[6]) #Rank after reranking
				#if the rank after reranking is 1 (most relevant) and
				#it's been moved to position 'rank'
				#i.e. aggregate all sets with most relevant in position
				#'rank' into [relevant_sets]
				if y[6] == '1' and y[3] == str(rank):
					relevant_sets[x] = entries[x]


		out = []
		for x in relevant_sets.keys():
			if str(int(x)) in entries:
				for y in entries[str(int(x))]:
					out.append(y)

		with open("MostRelevantinRank" + str(rank) + ".csv","w") as o:
	     		w = csv.writer(o, delimiter="\t",lineterminator="\n")
			for x in out:
				w.writerow(x)
		
		print "Found " + str(len(relevant_sets)) + " relevant sets with most related shuffled to position " + str(rank)
				


